<?php

namespace Acme\DemoBundle\Controller;

use Symfony\Component\HttpFoundation\Session\Session;

use Acme\DemoBundle\DependencyInjection\MyUser;

use Symfony\Component\Security\Core\SecurityContext;

use Acme\DemoBundle\Form\FormLogin;

use Symfony\Component\HttpFoundation\Response;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

// these import the "@Route" and "@Template" annotations
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

class HomeSecuredController extends Controller
{

    public function loginAction() {
        $form = $this->createForm(new FormLogin());
        // option 2
        //$form = $this->get('form.factory')->create(new FormLogin());

        $request = $this->get('request');

        if ($request->isMethod('POST')) {

            // get form
            $form->submit($request);

            // check valid
            if ($form->isValid()) {
                $strUser = $form["user_name"]->getData();
                $strPass = $form["password"]->getData();
                $objUser = self::getUserInfo($strUser, $strPass);
                if($objUser) {
                    $myUser = new MyUser();
                    $session = new Session();
                    $myUser->setUser($objUser, $session);
                    return $this->redirect($this->generateUrl('_home'));

                } else {

                    return $this->render('AcmeDemoBundle:HomeSecured:login.html.twig', array(
                            'form' => $form->createView(),
                            'error_message' => 'Invalid user'
                    ));
                }
            }
        }
        return $this->render('AcmeDemoBundle:HomeSecured:login.html.twig', array(
                            'form' => $form->createView(),

                    ));

    }

    /**
     * get user
     * @param unknown_type $strUser
     * @param unknown_type $strPass
     * @return ary User
     */
    public function getUserInfo($strUser, $strPass)
    {
        $objManager = $this->getDoctrine()->getManager();
        try {
//             $strSQL = " Select * From t_user ";
//             $strSQL .= " Where user_name = '" . $strUser . "'";
//             $strSQL .= " And password = '" . md5($strPass) . "'";
//             $strSQL .= " And is_deleted = false ";
//             $statement = $objManager->getConnection()->prepare($strSQL);
//             $statement->execute();
//             $results = $statement->fetchAll();
//             return $results;

            return $objManager->getRepository('AcmeDemoBundle:TUser')->findOneBy(array(
                                        'user_name' => $strUser,
                                        'password' => md5($strPass),
                                        'is_deleted' => false,
            ));

        } catch (Exception $e) {
            $objManager->getConnection()->rollback();
            var_dump($e);die;
        }
    }

}
